package com.example.videoweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.videoweb.domain.UnCheckVideo;
import com.example.videoweb.domain.Video;
import com.example.videoweb.service.ConsumerService;
import com.example.videoweb.service.UnCheckVideoService;
import com.example.videoweb.service.VideoService;
import com.example.videoweb.utils.methods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;


@RestController
@RequestMapping("/unCheckVideo")
public class UnCheckVideoController {

    @Autowired
    private UnCheckVideoService unCheckVideoService;

    @Autowired
    private VideoService videoService;

    @Autowired
    private ConsumerService consumerService;

    //添加视频
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Object addVideo(@RequestParam("file") MultipartFile file[],
                           @RequestParam("authorId") String authorId,
                           @RequestParam("videoName") String videoName,
                           @RequestParam("duration") String duration,
                           @RequestParam("style") String style1,
                           @RequestParam("introduction") String introduction){

        Byte style = methods.changeStyle(style1);
        JSONObject jsonObject = new JSONObject();

        if(file[0].isEmpty()||file[1].isEmpty()){
            jsonObject.put("code",0);
            jsonObject.put("msg","文件上传失败");
            return jsonObject;
        }

        if(videoName == null||videoName.equals("")){
            jsonObject.put("code",0);
            jsonObject.put("msg","视频名不能为空");
            return jsonObject;
        }

        //文件名=当前时间的毫秒+原来的文件名
        String fileName1 = System.currentTimeMillis()+file[0].getOriginalFilename();  //视频
        String fileName2 = System.currentTimeMillis()+file[1].getOriginalFilename();  //封面
        //文件路径
        String filePath1 = System.getProperty("user.dir")+System.getProperty("file.separator")+"video";
        String filePath2 = System.getProperty("user.dir")+System.getProperty("file.separator")+"videoPic";
        //如果文件路径不存在，新增该路径
        File file1 = new File(filePath1);
        File file2 = new File(filePath2);
        if(!file1.exists()){
            file1.mkdir();
        }
        if(!file2.exists()){
            file2.mkdir();
        }

        //实际的文件地址
        File dest1 = new File(filePath1+System.getProperty("file.separator")+fileName1);
        File dest2 = new File(filePath2+System.getProperty("file.separator")+fileName2);
        //存储到数据库里的相对文件地址
        String storeVideoPicPath = "/video/"+fileName1;
        String storeVideoPath = "/videoPic/"+fileName2;
        //作者名
        String authorName = consumerService.selectByPrimaryKey(Integer.parseInt(authorId)).getUsername();
        try {
            file[0].transferTo(dest1);
            file[1].transferTo(dest2);

            UnCheckVideo unCheckVideo = new UnCheckVideo();
            unCheckVideo.setAuthorId(Integer.parseInt(authorId));
            unCheckVideo.setAuthorName(authorName);
            unCheckVideo.setVideoName(videoName);
            unCheckVideo.setDuration(Integer.parseInt(duration));
            unCheckVideo.setStyle(new Byte(style));
            unCheckVideo.setIntroduction(introduction);
            unCheckVideo.setPic(storeVideoPath);
            unCheckVideo.setUrl(storeVideoPicPath);

            boolean flag = unCheckVideoService.insert(unCheckVideo);
            if (flag) {
                jsonObject.put("code",1);
                jsonObject.put("msg","上传成功");
                return jsonObject;
            }
            jsonObject.put("code",0);
            jsonObject.put("msg","上传失败");
            return jsonObject;
        } catch (IOException e) {
            jsonObject.put("code",0);
            jsonObject.put("msg","上传失败"+e.getMessage());
        }finally {
            return jsonObject;
        }
    }

    //视频审核成功
    @RequestMapping(value = "/addToVideo",method = RequestMethod.GET)
    public Object addToVideo(HttpServletRequest request){
        JSONObject jsonObject = new JSONObject();
        String id = request.getParameter("id");

        UnCheckVideo unCheckVideo = unCheckVideoService.selectByPrimaryKey(Integer.parseInt(id));
        Video video = new Video();
        video.setAuthorId(unCheckVideo.getAuthorId());
        video.setAuthorName(unCheckVideo.getAuthorName());
        video.setVideoName(unCheckVideo.getVideoName());
        video.setDuration(unCheckVideo.getDuration());
        video.setStyle(unCheckVideo.getStyle());
        video.setIntroduction(unCheckVideo.getIntroduction());
        video.setPic(unCheckVideo.getPic());
        video.setUrl(unCheckVideo.getUrl());
        video.setPraise(0);
        video.setPlayNumber(0);

        boolean flag = videoService.insert(video);
        if (flag) {
            unCheckVideoService.delete(Integer.parseInt(id));
            jsonObject.put("code",1);
            jsonObject.put("msg","审核通过");
            return jsonObject;
        }
        jsonObject.put("code",0);
        jsonObject.put("msg","审核失败");
        return jsonObject;
    }

    //视频拒绝成功
    @RequestMapping(value = "/refuse",method = RequestMethod.POST)
    public Object refuse(HttpServletRequest request){
        JSONObject jsonObject = new JSONObject();
        String id = request.getParameter("id");
        String cause = request.getParameter("cause");

        UnCheckVideo unCheckVideo = new UnCheckVideo();
        unCheckVideo.setId(Integer.parseInt(id));
        unCheckVideo.setChecked(new Byte("1"));
        unCheckVideo.setCause(cause);

        boolean flag = unCheckVideoService.update(unCheckVideo);
        if (flag) {
            jsonObject.put("code",1);
            jsonObject.put("msg","拒绝成功");
            return jsonObject;
        }
        jsonObject.put("code",0);
        jsonObject.put("msg","拒绝失败");
        return jsonObject;
    }

    //修改视频信息
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object updateVideo(@RequestParam("file") MultipartFile file[],
                              @RequestParam("id") Integer id,
                              @RequestParam("authorId") String authorId,
                              @RequestParam("videoName") String videoName,
                              @RequestParam("duration") String duration,
                              @RequestParam("style") String style1,
                              @RequestParam("introduction") String introduction,
                              @RequestParam("checked") String checked) {
        Byte style = methods.changeStyle(style1);
        JSONObject jsonObject = new JSONObject();

        if(videoName == null||videoName.equals("")){
            jsonObject.put("code",0);
            jsonObject.put("msg","视频名不能为空");
            return jsonObject;
        }

        UnCheckVideo unCheckVideo = new UnCheckVideo();

        if(!file[0].isEmpty()){  //视频
            //删除原视频
            String url = unCheckVideoService.selectByPrimaryKey(id).getUrl();
            File file0=new File(System.getProperty("user.dir")+"/"+url);
            if(file0.exists() && file0.isFile()){
                file0.delete();
            }

            String fileName1 = System.currentTimeMillis()+file[0].getOriginalFilename();
            String filePath1 = System.getProperty("user.dir")+System.getProperty("file.separator")+"video";
            File file1 = new File(filePath1);
            if(!file1.exists()){
                file1.mkdir();
            }
            File dest1 = new File(filePath1+System.getProperty("file.separator")+fileName1);
            String storeVideoPath = "/video/"+fileName1;
            try {
                file[0].transferTo(dest1);
            } catch (IOException e) {
                e.printStackTrace();
            }
            unCheckVideo.setUrl(storeVideoPath);
            unCheckVideo.setDuration(Integer.parseInt(duration));
        }
        if(!file[1].isEmpty()){//封面
            //删除原封面
            String pic = unCheckVideoService.selectByPrimaryKey(id).getPic();
            File file0=new File(System.getProperty("user.dir")+"/"+pic);
            if(file0.exists() && file0.isFile()){
                file0.delete();
            }

            String fileName2 = System.currentTimeMillis()+file[1].getOriginalFilename();
            String filePath2 = System.getProperty("user.dir")+System.getProperty("file.separator")+"videoPic";
            File file2 = new File(filePath2);
            if(!file2.exists()){
                file2.mkdir();
            }
            File dest2 = new File(filePath2+System.getProperty("file.separator")+fileName2);
            String storeVideoPicPath = "/videoPic/"+fileName2;
            try {
                file[0].transferTo(dest2);
            } catch (IOException e) {
                e.printStackTrace();
            }
            unCheckVideo.setPic(storeVideoPicPath);
        }

        //作者名
        String authorName = consumerService.selectByPrimaryKey(Integer.parseInt(authorId)).getUsername();

        unCheckVideo.setId(id);
        unCheckVideo.setAuthorId(Integer.parseInt(authorId));
        unCheckVideo.setAuthorName(authorName);
        unCheckVideo.setVideoName(videoName);
        unCheckVideo.setStyle(new Byte(style));
        unCheckVideo.setIntroduction(introduction);
        if (checked != null){
            unCheckVideo.setChecked(new Byte(checked));
        }
        boolean flag = unCheckVideoService.update(unCheckVideo);
        if (flag) {
            jsonObject.put("code",1);
            jsonObject.put("msg","上传成功");
            return jsonObject;
        }
        jsonObject.put("code",0);
        jsonObject.put("msg","上传失败");
        return jsonObject;
    }

    //删除视频
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public Object deleteVideo(HttpServletRequest request){
        String id = request.getParameter("id").trim();
        return unCheckVideoService.delete(Integer.parseInt(id));

    }

    //根据id查询视频
    @RequestMapping(value = "/getById",method = RequestMethod.GET)
    public Object getById(HttpServletRequest request){
        String id = request.getParameter("id").trim();
        return unCheckVideoService.selectByPrimaryKey(Integer.parseInt(id));
    }

    //根据作者id查询视频
    @RequestMapping(value = "/getByAuthorId",method = RequestMethod.GET)
    public Object getByAuthorId(HttpServletRequest request){
        String authorId = request.getParameter("authorId").trim();
        return unCheckVideoService.getByAuthorId(Integer.parseInt(authorId));
    }

    //查询所有未审核视频
    @RequestMapping(value = "/allVideo",method = RequestMethod.GET)
    public Object allVideo(HttpServletRequest request){
        return unCheckVideoService.allVideo();
    }

}


